/**
 * 题目一:
 * 将下面异步代码使用 Promise 的方式改进
 *
 * setTimeout(function() {
 *   var a = 'hello';
 *   setTimeout(function() {
 *     var b = 'lagou';
 *     setTimeout(function() {
 *       var c = 'I ❤ U';
 *       console.info(a + b + c);
 *     }, 10);
 *   }, 10);
 * }, 10);
 */

/**
 * 思路1:
 * 将 a, b, c 获取封装为 Promise 形式, 通过 Promise.all 一并获取
 */

const fetchA = () =>
  new Promise((resolve) => setTimeout(() => resolve('hello'), 10));
const fetchB = () =>
  new Promise((resolve) => setTimeout(() => resolve('lagou'), 10));
const fetchC = () =>
  new Promise((resolve) => setTimeout(() => resolve('I ❤ U'), 10));

Promise.all([fetchA(), fetchB(), fetchC()]).then(([a, b, c]) =>
  console.log(`${a}${b}${c}`)
);

/**
 * 思路2:
 * 将 a, b, c 获取封装为 Promise 形式, 通过 then 链式调用
 */
[fetchA, fetchB, fetchC]
  .reduce((ps, fetch) => {
    const next = fetch();
    return ps.then((result) => next.then((val) => result.concat([val])));
  }, Promise.resolve([]))
  .then(([a, b, c]) => {
    console.log(`${a}${b}${c}`);
  });
